<template>
	<view class="container">
		<!-- #ifdef APP-PLUS || H5 -->
		<view class="header">
			<view>您共购买过</view>
			<view class="coupon-num">{{ total }}张</view>
			<view>可使用的会员卡</view>
		</view>
		<!-- #endif -->
		<!-- #ifdef MP -->
		<cover-view class="header">
			<cover-view>您共购买过</cover-view>
			<cover-view class="coupon-num">{{ total }}张</cover-view>
			<cover-view>可使用的会员卡</cover-view>
		</cover-view>
		<!-- #endif -->

		<view class="coupon-list">
			<view class="coupon-item" v-for="(item, index) in records" :key="index">
				<view class="coupon">
					<image src="/static/images/index/img_fuwuquan_blue_3x.png" class="coupon-img"></image>
					<view class="left-tit-box">
						<view class="tit">{{ item.cardName }}</view>
						<view class="term">{{ item.status===0?"未支付":"已支付" }}|{{ getPayTypeStr(item.payType) }}</view>
					</view>
					<view class="right-detail" @tap="spread(index)">
						<text class="detail-txt">查看详情</text>
						<image src="/static/images/index/icon_next_3x.png" class="arrow" :class="{ 'tui-rotate_90': item.spread }"></image>
					</view>
				</view>
				<view class="hidden-box" v-show="item.spread">
					<view class="code-tit">展示付款码给店员完成支付</view>
					<view class="code-num">卡号：{{ item.cardNo }}</view>
					<view class="qrcode-box">
						<view class="qrcode">
							<canvas :class="{ 'my-canvas': !item.spread }" :style="{ width: qrcode_w + 'px', height: qrcode_w + 'px' }"
							 :canvas-id="item.id" :id="item.id"></canvas>
						</view>
					</view>
					<view class="list-item">
						<view class="item-tit">使用次数</view>
						<view class="item-con">{{ item.total }}次</view>
					</view>
					<view class="list-item">
						<view class="item-tit">价格</view>
						<view class="item-con">￥{{ item.payPrice }}</view>
					</view>

					<view class="explain">使用说明</view>
					<view class="explain-text" v-for="(items, index) in item.useRemark" :key="items">{{ items }}</view>
					<!-- <view class="explain-text">{{ item.useRemark }}</view> -->
				</view>
			</view>
		</view>
		<view class="none" v-show="total == 0">还没有购买过会员卡~</view>
	</view>
</template>

<script>
	const qrCode = require('@/libs/weapp-qrcode.js');
	import {
		getMemberCardOrder
	} from '@/api/user.js';
	export default {
		data() {
			return {
				show: false,
				current: 1,
				pages: 1,
				size: 20,
				total: 0,
				records: [],

				qrcode_w: uni.upx2px(240)
			};
		},
		onLoad: function(options) {

			this.initData();
		},
		methods: {
			initData() {
				uni.showLoading({
					title: '正在加载…'
				});
				getMemberCardOrder().then(res => {
					uni.hideLoading();
					const {
						current,
						pages,
						total,
						records
					} = res.data;
					this.total = total;
					this.current = current;
					this.pages = pages;
					for (var i = 0; i < records.length; i++) {
						records[i].spread = false;
						if (i == 0) {
							records[i].spread = true;
						}
						records[i].useRemark = records[i].useRemark.split('\n');
						this.records.push(records[i]);
					}
					setTimeout(() => {
						let params = {
							id:this.records[0].id,
							type:1//0会员卡使用 1 购卡 2 商品 
						}
						this.couponQrCode(JSON.stringify(params), this.records[0].id);
					}, 60);

				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			},
			getPayTypeStr(payType) {
				//0 线下 1 小程序 2 公众号 3 H5 4 PC 5 支付宝 6 余额
				let tmp = "线下支付";
				switch (payType) {
					case 6:
						tmp = "余额支付";
						break;
					case 5:
						tmp = "支付宝支付";
						break;
					case 4:
						tmp = "微信支付";
						break;
					case 3:
						tmp = "微信支付";
						break;
					case 2:
						tmp = "微信支付";
						break;
					case 1:
						tmp = "微信支付";
						break;
					default:
						tmp = "线下支付";
						break;
				}
				return tmp;
			},
			spread: function(index) {
				let records = this.records;
				if (!records[index].spread) {
					setTimeout(() => {
						let params = {
							id:records[index].id,
							type:1//0会员卡使用 1 购卡 2 商品 
						}
						this.couponQrCode(JSON.stringify(params), records[index].id);
					}, 60);
				}
				records[index].spread = !records[index].spread;
				this.records = records;
			},
			// 二维码生成工具
			couponQrCode(text, canvasId) {
				new qrCode(canvasId, {
					text: text,
					width: this.qrcode_w,
					height: this.qrcode_w,
					colorDark: '#333333',
					colorLight: '#FFFFFF',
					correctLevel: qrCode.CorrectLevel.H
				});
			}
		}
	};
</script>

<style>
	/* #ifdef MP-ALIPAY */
	.my-canvas {
		position: fixed;
		left: -200px;
		top: 0;
	}

	/* #endif */

	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.header {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 24rpx;
		color: #999;
		height: 54rpx;
		line-height: 54rpx;
		background: #fff;
		display: flex;
		align-items: center;
		position: fixed;
		top: 0;
		/* #ifdef H5 */
		top: 44px;
		/* #endif */
		z-index: 999999;
	}

	.coupon-num {
		color: #5677fc;
	}

	.coupon-list {
		width: 100%;
		padding: 54rpx 50rpx;
		box-sizing: border-box;
	}

	.coupon-item {
		margin-top: 20rpx;
		width: 100%;
		box-shadow: 0 8rpx 10rpx -8rpx #eaeef1;
		border-radius: 2rpx;
	}

	.coupon {
		height: 160rpx;
		position: relative;
		display: flex;
		align-items: center;
	}

	.coupon-img {
		width: 100%;
		height: 160rpx;
		position: absolute;
		left: 0;
		top: 0;
		z-index: 0;
	}

	.left-tit-box {
		width: 414rpx;
		padding-left: 75rpx;
		box-sizing: border-box;
		z-index: 10;
	}

	.tit {
		width: 290rpx;
		font-size: 30rpx;
		color: #333;
		line-height: 42rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.term {
		width: 228rpx;
		font-size: 24rpx;
		color: #999;
		line-height: 24rpx;
		padding-top: 7rpx;
		white-space: nowrap;
	}

	.right-detail {
		width: 236rpx;
		text-align: center;
		height: 160rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
	}

	.detail-txt {
		font-size: 24rpx;
		color: #fff;
		padding-right: 12rpx;
	}

	.arrow {
		width: 16rpx;
		height: 24rpx;
		transition: all 0.15s ease;
	}

	.spread {
		width: 24rpx;
		height: 16rpx;
	}

	.qrcode {
		width: 260rpx;
		height: 260rpx;
		margin: 45rpx auto 43rpx auto;
	}

	.hidden-box {
		padding: 35rpx 40rpx 27rpx 40rpx;
		box-sizing: border-box;
		color: #333;
		border-top: 1rpx solid #eaeef1;
		background: #fff;
	}

	.code-tit {
		font-size: 28rpx;
		line-height: 28rpx;
	}

	.code-num {
		font-size: 30rpx;
		line-height: 30rpx;
		padding-top: 23rpx;
		font-weight: bold;
	}

	.qrcode-box {
		width: 100%;
		border-bottom: 1rpx dashed #eaeef1;
		margin-bottom: 6rpx;
	}

	.list-item {
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
	}

	.list-item::after {
		left: 0;
	}

	.item-tit {
		color: #666;
	}

	.item-con {
		color: #333;
	}

	.explain {
		font-size: 28rpx;
		line-height: 28rpx;
		color: #666;
		padding: 33rpx 0 25rpx 0;
	}

	.explain-text {
		font-size: 28rpx;
		line-height: 41rpx;
		color: #333;
	}

	.none {
		width: 100%;
		position: fixed;
		text-align: center;
		font-size: 30rpx;
		color: #ccc;
		top: 49%;
	}

	.tui-rotate_90 {
		transform: rotate(90deg);
	}
</style>
